<?php

    class TipoProducto {
        private $id          = null;
        private $linea       = null;
        private $descripcion = null;

        function __construct() {}

        function get_id                 () { return $this->id;          }
        function get_linea              () { return $this->linea;       }
        function get_descripcion        () { return $this->descripcion; }

        function set_id                 ( $value ) { $this->id          = $value; }
        function set_linea              ( $value ) { $this->linea       = $value; }
        function set_descripcion        ( $value ) { $this->descripcion = $value; }

        static function GetCollection() {
            $dbh = DBH::GetInstance()->get_dbh();

            $sth = $dbh->query(
                'SELECT     tp.id, tp.id_linea, tp.descripcion
                FROM        tipos_productos AS tp
                INNER JOIN  lineas AS l ON tp.id_linea = l.id
                ORDER BY    tp.descripcion, l.nombre'
            );

            $collection = array();
            foreach( $sth->fetchAll(PDO::FETCH_ASSOC) as $row ) {
                $instance = new TipoProducto();
                $instance->set_id( $row['id'] );
                $instance->set_linea( Linea::GetByID($row['id_linea']) );
                $instance->set_descripcion( $row['descripcion'] );
                array_push( $collection, $instance );
            }

            return $collection;
        }

        static function GetByID( $id ) {
            $dbh = DBH::GetInstance()->get_dbh();

            $sth = $dbh->prepare('SELECT id, id_linea, descripcion FROM tipos_productos WHERE id = :id');
            $sth->bindParam( ':id', $id, PDO::PARAM_INT );
            $sth->execute();
            $result = $sth->fetch( PDO::FETCH_ASSOC );

            $instance = new TipoProducto();
            $instance->set_id( $result['id'] );
            $instance->set_linea( Linea::GetByID($result['id_linea']) );
            $instance->set_descripcion( $result['descripcion'] );

            return $instance;
        }

        static function GetByLinea( $id_linea ) {
            $dbh = DBH::GetInstance()->get_dbh();

            $sth = $dbh->prepare(
                'SELECT     tp.id, tp.id_linea, tp.descripcion
                FROM        tipos_productos AS tp
                INNER JOIN  lineas AS l ON tp.id_linea = l.id
                WHERE       tp.id_linea = :id_linea
                ORDER BY    tp.descripcion, l.nombre'
            );
            $sth->bindParam( ':id_linea', $id_linea, PDO::PARAM_INT );
            $sth->execute();

            $collection = array();
            foreach( $sth->fetchAll(PDO::FETCH_ASSOC) as $row ) {
                $instance = new TipoProducto();
                $instance->set_id( $row['id'] );
                $instance->set_linea( Linea::GetByID($row['id_linea']) );
                $instance->set_descripcion( $row['descripcion'] );
                array_push( $collection, $instance );
            }

            return $collection;
        }
    }

?>